package view;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.devgroupvn.dota.NEWSAdapter;
import com.devgroupvn.dota.NewsArchive;
import com.devgroupvn.dota.WebViewNews;
import com.devgroupvn.dota.webviewloaddata;
import com.devgroupvn.dota.items.GetItems;
import com.devgroupvn.dota.items.Item;
import com.devgroupvn.dota.items.ItemListAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

public class ItemListLayout extends LinearLayout
{
	ListView mListView;
	List<Item> ListItem = new LinkedList<Item>();
	ItemListAdapter adapter;
	Context mContext;
	GetItems getItem = new GetItems();
	public ItemListLayout(Context context)
	{
		super(context);
		mContext = context;
		init();
		action();
		new AsyntaskLoadItemList().execute();
	}
	private void init()
	{
		mListView = new ListView(getContext());
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		addView(mListView, params);
		adapter = new ItemListAdapter(mContext, ListItem);
		mListView.setAdapter(adapter);
	}
	private void action()
	{
		mListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> a, View v, int position, long id)
			{
				Item entry = ListItem.get(position);
//				String des = getItem.getItemDetail(entry.url);
				
				Intent intent = new Intent(mContext, webviewloaddata.class);
				intent.putExtra("link", entry.url);
				mContext.startActivity(intent);
			}

		});
	}
	
	public class AsyntaskLoadItemList extends AsyncTask<Void, Void, Void>
	{

		@Override
		protected Void doInBackground(Void... params)
		{
			ListItem.addAll(getItem.getItemsList());
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			if (ListItem != null)
			{
				adapter.notifyDataSetChanged();
			}
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
		}

	}


}
